package com.ray.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.google.appengine.api.search.query.ExpressionParser.negation_return;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.OrderCustomTagBean;
import com.ray.bean.OrderDeviceBean;
import com.ray.bean.OrderInfoDetailBean;
import com.ray.bean.OrderInfoTemplateBean;
import com.ray.exception.ServiceException;
import com.ray.mapper.BasicDataMapper;
import com.ray.mapper.FileManagerMapper;
import com.ray.mapper.MaterialTypeExceptSourceMapper;
import com.ray.mapper.OrderCreateMapper;
import com.ray.util.ArrayUtils;
import com.ray.util.callback.CallBackBody;
import com.ray.util.callback.CallBackTask;

import net.sf.json.JSONArray;
/**
 * 
 * @author Shensg
 * 2018年1月26日
 */
@Service
public class OrderCreateService {
	@Autowired
	OrderCreateMapper orderCreateMapper;
	@Autowired
	OrderCreateApplyService orderCreateApplyService;
	@Autowired
	BasicDataMapper basicDataMapper;
	@Autowired
	FileManagerMapper fileManagerMapper;
	@Autowired
	MaterialTypeExceptSourceMapper materialTypeExceptSouceMapper;
	@Autowired
	PurchaseOrderService purchaseOrderService;
	@Autowired
	OrderProcedureTemplateService orderProcedureTemplateService;
	
	public List<Map> getOrderTabList(Map map) {
		String type = map.get("type").toString();
		if(!"0".equals(type)&&!"4".equals(type)) {//不是订单信息
			return basicDataMapper.getOrderTabListForSourceFactory(map);
		}
		return orderCreateMapper.getOrderTabList(map);
		/*
		if("1".equals(type)) {
			map.remove("type");
			map.put("type","原材料仓");
			//return basicDataMapper.getOrderTabList(map);
			return basicDataMapper.getOrderTabListForSourceFactory(map);
		}
		if("2".equals(type)) {
			map.remove("type");
			map.put("type","辅料仓");
			return basicDataMapper.getOrderTabList(map);
		}
		if("3".equals(type)) {
			map.remove("type");
			map.put("type","信息备注仓");
			MaterialTypeExceptSourceBean bean = new MaterialTypeExceptSourceBean();
			bean.setCompany(map.get("loginCompany").toString());
			bean.setType("3");
			if(map.get("order_no")!=null) {
				bean.setOrderNo(map.get("order_no").toString());
			}
			return materialTypeExceptSouceMapper.getListForCreateOrder(bean);
		}
		return orderCreateMapper.getOrderTabList(map);*/
	}

	public List<Map> getFactoryTabChildList(Map<String, String> map) {
		// TODO Auto-generated method stub
		//不存在isReadColor则通过程序读取是否读颜色
		if(map.get("isReadColor")==null||map.get("isReadColor").toString().trim().length()<1||map.get("isReadColor").toString().trim().equals("0")) {
			Map<String, String> materialTypeInfo = orderCreateMapper.getMaterialTypeInfo(map);
			String isReadColor=materialTypeInfo.get("isReadColor");
			map.put("isReadColor", isReadColor);
		}
		if("1".equals(map.get("material_factory"))){
			map.remove("material_factory");
			map.put("material_factory","原材料仓");
		}
		if("2".equals(map.get("material_factory"))){
			map.remove("material_factory");
			map.put("material_factory","原材料仓,辅料仓");
		}
		if("3".equals(map.get("material_factory"))){
			map.remove("material_factory");
			map.put("material_factory","信息备注仓");
		}
		List<Map> retMap = orderCreateMapper.getFactoryTabChildList(map);
		for (int i = 0; i < retMap.size(); i++) {
			double useableCount = purchaseOrderService.getMaterialAvailableCount(retMap.get(i).get("materialCode").toString(), map.get("loginCompany"));
			retMap.get(i).put("useableMaterialCount", useableCount);
		}
		return retMap;
	}
	//保存html 模版
	public int saveAsOrderTemplate(OrderInfoTemplateBean bean) {
		// TODO Auto-generated method stub
		return orderCreateMapper.saveAsOrderTemplate(bean);
	}
	//获取基础数据列表
	public List<Map> getDataBasicList(Map map) {
		// TODO Auto-generated method stub
		return orderCreateMapper.getDataBasicList(map);
	}

	public void updateDataBasicById(Map map) {
		// TODO Auto-generated method stub
		String str = map.get("html_type_val").toString().replaceAll("，", ",");
		map.put("html_type_val", str);
		orderCreateMapper.updateDataBasicById(map);
	}
	
	//保存订单信息
	public boolean saveOrderInfo(Map map) throws ServiceException {
		String save_as_new_order=map.get("save_as_new_order").toString();
		String order_no = map.get("order_p_order_no").toString();
		//制单人
		if(map.get("order_p_created")==null||map.get("order_p_created").toString().trim().length()<1) {
			map.put("order_p_created", map.get("loginUserName"));
		}
		if("1".equals(save_as_new_order)) {
			order_no=(String) orderCreateMapper.getOrderInfoFixed(map).get("result");
			//fileManagerMapper.updateLastOneOrderNoByUserName(order_no,map);
			//如果是新单
			map.put("order_p_created", map.get("loginUserName"));
		}
		if(order_no.indexOf("BS")>-1) {
			String linkOrderNo=order_no;
			int type=1;
			order_no=(String) orderCreateMapper.getOrderInfoFixed(map).get("result");
			//fileManagerMapper.updateLastOneOrderNoByUserName(order_no,map);
			//记录业务单和生产单关联关系
			orderCreateMapper.saveOrderLink(order_no,type,linkOrderNo);
		}
		map.put("order_p_order_no",order_no);
		if(map.get("order_p_audit")!=null) {
			if("true".equals(map.get("order_p_audit").toString())) {
				map.put("order_p_audit", "1");
			}else {
				map.put("order_p_audit", "0");
			}
		}
		if(map.get("order_p_order_no_comp")!=null) {
			//客户订单编号 如果没有 和订单编号一样 ，有取自己的
			if("".equals(map.get("order_p_order_no_comp").toString().trim())) {
				map.put("order_p_order_no_comp", order_no);
			}
		}
		//订单提交 审核不选 订单状态默认计划
		map.put("order_p_order_status", "计划");
		map.put("order_p_audit", "0");
		
		map.put("audit_type", "1");//生产订单审核 项目为1 
		/*if(orderCreateMapper.checkAuditTheSameOne(map)>0) {
			map.put("order_p_order_status", "审核");
			map.put("order_p_audit", "1");
		}*/
		
		String order_template_id = map.get("order_template_id").toString();
		
		//删除原有订单信息 生产订单主表, order_info_detail order_info_tail order_custom_tag
		int id = orderCreateMapper.deleteOrderInfo(order_no);
		//保存生产订单主表
		int id_primary = orderCreateMapper.saveOrderInfo(map);
		//保存生产订单明细表
		List<OrderInfoDetailBean> list_orderDetails = getOrderInfoDetailBean(map,order_no);
		//保存自定义表单
		List<OrderCustomTagBean> list_orderCustomTag = getOrderCustomTagBean(map,order_no);
		
		//保存生产订单明细表 数据过大拆分
		List<OrderInfoDetailBean> list_orderDetails_new= new ArrayList<>();
		for (int i = 0; i < list_orderDetails.size(); i++) {
			list_orderDetails_new.add(list_orderDetails.get(i));
			if(list_orderDetails_new.size()==20) {//每20条数据拆分
				int id_detail = orderCreateMapper.saveOrderInfoDetail(list_orderDetails_new);
				list_orderDetails_new.clear();
			}
		}
		if(list_orderDetails_new.size()>0) {//如果少于20条
			int id_detail = orderCreateMapper.saveOrderInfoDetail(list_orderDetails_new);
			list_orderDetails_new.clear();
		}
		//保存生产订单明细-tail表
		List<OrderInfoDetailBean> list_orderTails = getOrderInfoTailBean(map,order_no);
		int id_tail = orderCreateMapper.saveOrderInfoTail(list_orderTails);
		//如果有data_classification 则使用分类仓保存
		List<OrderInfoDetailBean> list_data_classification = getOrderInfoTailBeanClassification(map,order_no);
		orderCreateMapper.saveOrderInfoTail(list_data_classification);
		//当投料数大于0保存一份到order_info_detail
		orderCreateMapper.saveOrderInfoDetailFromTail(order_no,map.get("order_p_order_total").toString());
		//保存自定义标签
		int id_customTag = orderCreateMapper.saveOrderCustomTag(list_orderCustomTag);
		//保存模版和订单编号
		if(Integer.valueOf(order_template_id)!=0) {
			orderCreateMapper.deleteOrderInfoAndTemplate(order_no);
			int id_order_template = orderCreateMapper.saveOrderInfoAndTemplate(order_no,order_template_id);
		}
		//插入companyMaterial 不再保存20180404
		/*for(OrderInfoDetailBean bean:list_orderDetails) {
			if(bean.getMaterialFactory()=="1") {
				bean.setMaterialFactory("原材料仓");
			}else if(bean.getMaterialFactory()=="2") {
				bean.setMaterialFactory("辅料仓");
			}else {
				bean.setMaterialFactory("信息备注仓");
			}
			bean.setCompany(map.get("loginCompany").toString());
			if(bean.getMaterialCode()!=null&&bean.getMaterialCode().trim().length()!=0) {//有code insert into select
				orderCreateMapper.insertCompanyMTbyCode(bean.getMaterialCode(),bean.getCompany());
			}else {
				orderCreateMapper.insertCompanyMTbyBean(bean);
			}
		}
		for(OrderInfoDetailBean bean:list_orderTails) {
			if(bean.getMaterialFactory()=="1") {
				bean.setMaterialFactory("原材料仓");
			}else if(bean.getMaterialFactory()=="2") {
				bean.setMaterialFactory("辅料仓");
			}else {
				bean.setMaterialFactory("信息备注仓");
			}
			bean.setCompany(map.get("loginCompany").toString());
			if(bean.getMaterialCode()!=null&&bean.getMaterialCode().trim().length()!=0) {//有code insert into select
				orderCreateMapper.insertCompanyMTbyCode(bean.getMaterialCode(),bean.getCompany());
			}else {
				orderCreateMapper.insertCompanyMTbyBean(bean);
			}
		}*/
		map.put("order_no",order_no);
		orderCreateApplyService.sendTemplateMsg("apply",map);
		return true;
	}
	
	//获取自定义表单信息数据 组装 当前只有一条数据
	private List<OrderCustomTagBean> getOrderCustomTagBean(Map map, String order_no) {
		List<OrderCustomTagBean> list = new ArrayList<>();
		OrderCustomTagBean bean =new OrderCustomTagBean();
		if(map.get("order_label_label_val")!=null) {
			bean.setLabel(map.get("order_label_label_val").toString());
		}
		if(map.get("order_label_table_val")!=null) {
			bean.setTable(map.get("order_label_table_val").toString());
		}
		if(map.get("order_label_table_val_two")!=null) {
			bean.setTableTwo(map.get("order_label_table_val_two").toString());
		}
		if(map.get("order_label_table_val_three")!=null) {
			bean.setTableThree(map.get("order_label_table_val_three").toString());
		}
		if(map.get("order_label_table_val_four")!=null) {
			bean.setTableFour(map.get("order_label_table_val_four").toString());
		}
		if(map.get("order_label_table_val_five")!=null) {
			bean.setTableFive(map.get("order_label_table_val_five").toString());
		}
		//6
		if(map.get("order_label_table_val_six")!=null) {
			bean.setTableSix(map.get("order_label_table_val_six").toString());
		}
		//7
		if(map.get("order_label_table_val_seven")!=null) {
			bean.setTableSeven(map.get("order_label_table_val_seven").toString());
		}
		//8
		if(map.get("order_label_table_val_eight")!=null) {
			bean.setTableEight(map.get("order_label_table_val_eight").toString());
		}
		//9
		if(map.get("order_label_table_val_nine")!=null) {
			bean.setTableNine(map.get("order_label_table_val_nine").toString());
		}
		//10
		if(map.get("order_label_table_val_ten")!=null) {
			bean.setTableTen(map.get("order_label_table_val_ten").toString());
		}
		bean.setOrderNo(order_no);
		list.add(bean);
		return list;
	}

	//获取订单明细尾巴Tail表
	private List<OrderInfoDetailBean> getOrderInfoTailBean(Map map, String orderNo) {
		List<OrderInfoDetailBean> list = new ArrayList<>();
		OrderInfoDetailBean bean = null;
		if(map.get("order_s_input_material_house")==null) {
			return list;
		}
		String[] materialHouse = map.get("order_s_input_material_house").toString().split(",");
		String[] materialType=map.get("order_s_input_material_type").toString().split(",");
		String[] materialModel=map.get("order_s_input_val_material_type").toString().split(",");
		String[] materialCode=map.get("order_s_input_material_code").toString().split(",");
		String[] feedTotal=null;
		if(map.get("order_s_input_val_feed_total")!=null)feedTotal=map.get("order_s_input_val_feed_total").toString().split(",");
		//增加工艺
		String[] arts=null;
		if(map.get("order_s_input_val_arts")!=null)arts=map.get("order_s_input_val_arts").toString().split(",");
		//增加材质
		String[] qualityType=null;
		if(map.get("order_s_input_val_quality_type")!=null)qualityType=map.get("order_s_input_val_quality_type").toString().split(",");
		//读取颜色 //20190330 去除模板控制物料类型
		/*String[] isReadColor=null;
		if (map.get("order_s_input_isReadColor")!=null) {
			isReadColor=map.get("order_s_input_isReadColor").toString().split(",");
		}*/
		//读取说明
		String[] remark=null;
		if (map.get("order_s_input_val_remark")!=null) {
			remark=map.get("order_s_input_val_remark").toString().split(",");
		}
		//读取供应商
		String[] providerName=null;
		if (map.get("order_s_input_val_provider_name")!=null) {
			providerName=map.get("order_s_input_val_provider_name").toString().split(",");
		}
		for(int i=0;i<materialType.length;i++) {
			bean = new OrderInfoDetailBean();
			bean.setMaterialFactory(materialHouse[i]);
			bean.setMaterialType(materialType[i]);
			//20190330 去除模板控制物料类型
			/*if (isReadColor!=null&&isReadColor[i].trim().length()>0) {
				bean.setIsReadColor(Integer.valueOf(isReadColor[i]));
			}*/
			//校验是否为0 避免新模板保存的时候出现的问题
			if(bean.getIsReadColor()==0) {
				map.put("material_type", bean.getMaterialType());
				map.put("material_factory", bean.getMaterialFactory());
				Map<String, String> materialTypeInfo = orderCreateMapper.getMaterialTypeInfo(map);
				bean.setIsReadColor(Integer.valueOf(materialTypeInfo.get("isReadColor")));
			}
			bean.setOrderNo(orderNo);
			bean.setMaterialModel(materialModel[i]);
			bean.setMaterialCode(materialCode[i].trim());
			if(feedTotal!=null) {
				bean.setFeedTotal((feedTotal[i].trim().length()>0)?feedTotal[i].trim():"0");//投料数
			}
			if(arts!=null) {
				bean.setArts((arts[i].trim().length()>0)?arts[i].trim():"");//工艺
			}
			if(qualityType!=null) {
				bean.setQualityType((qualityType[i].trim().length()>0)?qualityType[i].trim():"");//材质
			}
			if(remark!=null) {
				bean.setRemark((remark[i].trim().length()>0)?remark[i].trim():"");//说明
			}
			if(providerName!=null) {
				bean.setProviderName((providerName[i].trim().length()>0)?providerName[i].trim():"");//说明
			}
			//获取物料信息
			OrderInfoDetailBean beanByCode = orderCreateMapper.getMaterialInfoByCode(bean.getMaterialCode(),map);
			if(beanByCode!=null) {
				bean.setMaterialCode(beanByCode.getMaterialCode());//物料代码
				if(1==beanByCode.getIsReadColor()) {
					if (bean.getIsReadColor()==0) {
						bean.setIsReadColor(1);
					}
					bean.setColor(beanByCode.getColor());//色号
					bean.setColorDescription(beanByCode.getColorDescription());//描述
				}else {
					if (bean.getIsReadColor()==0) {
						bean.setIsReadColor(2);
					}
				}
				bean.setMaterialModel(beanByCode.getMaterialModel());//规格
				bean.setMaterialName(beanByCode.getMaterialName());//物料名称
			}
			list.add(bean);
		}
		return list;
	}
	
	//获取订单明细尾巴Tail表
	private List<OrderInfoDetailBean> getOrderInfoTailBeanClassification(Map map, String orderNo) {
		List<OrderInfoDetailBean> list = new ArrayList<>();
		OrderInfoDetailBean bean = null;
		JSONArray json = JSONArray.fromObject(map.get("data_classification") ); // 首先把字符串转成 JSONArray  对象
		if(json.size()>0){
		  for(int i=0;i<json.size();i++){
			bean = new OrderInfoDetailBean();
			net.sf.json.JSONObject job = json.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
			bean.setOrderNo(orderNo);
			bean.setMaterialType(job.has("material_type")?job.getString("material_type"):null);
			bean.setMaterialModel(job.has("material_model")?job.getString("material_model"):null);
			bean.setMaterialFactory(job.has("material_factory")?job.getString("material_factory"):null);
			bean.setMaterialCode(job.has("material_code")?job.getString("material_code"):null);
			bean.setFeedTotal(job.has("feed_total")?(job.getString("feed_total").length()>0?job.getString("feed_total"):"0"):"0");
			bean.setMaterialName(job.has("material_name")?job.getString("material_name"):null);
			bean.setColor(job.has("color")?job.getString("color"):null);
			bean.setColorDescription(job.has("color_description")?job.getString("color_description"):null);
			bean.setArts(job.has("arts")?job.getString("arts"):null);
			bean.setQualityType(job.has("quality_type")?job.getString("quality_type"):null);
			bean.setIsReadColor(1);
			bean.setRemark(job.has("remark")?job.getString("remark"):null);
			bean.setProviderName(job.has("provider_name")?job.getString("provider_name"):null);
			bean.setClassification(job.has("classification")?job.getString("classification"):null);
			list.add(bean);
		  }
		}
		return list;
	}
	
	//获取订单明细Bean
	private List<OrderInfoDetailBean> getOrderInfoDetailBean(Map map,String orderNo) {
		Map<String, String> lineNoCodeMap = getLineNoCode(map);
		List<OrderInfoDetailBean> list = new ArrayList<>();
		OrderInfoDetailBean bean = null;
		if(map.get("order_s_table_source_arr_str")==null||map.get("order_s_line_no")==null) {
			return list;
		}
		/*投料数*/
		String[] feedTotal = null;
		if(map.get("order_s_feed_total")!=null)feedTotal=map.get("order_s_feed_total").toString().split(",");
		/*行号*/
		String[] lineNo	= map.get("order_s_line_no").toString().split(",");
		/*物料代码*/
		String[] materialCode	= null;
		if(map.get("order_s_material_code")!=null)materialCode=map.get("order_s_material_code").toString().split(",");
		/*色号*/
		String[] materialColor= null;
		if(map.get("order_s_material_color")!=null)materialColor=map.get("order_s_material_color").toString().split(",");
		/*色号描述*/
		String[] materialColorDescription = null;
		if(map.get("order_s_material_description")!=null)materialColorDescription=map.get("order_s_material_description").toString().split(",");
		/*规格*/
		String[] materialModel=null;
		if(map.get("order_s_material_model")!=null)materialModel= map.get("order_s_material_model").toString().split(",");
		/*物料名称*/
		String[] materialName=null;
		if(map.get("order_s_material_name")!=null)materialName= map.get("order_s_material_name").toString().split(",");
		/*物料类型*/
		String[] materialType=null;
		if(map.get("order_s_material_type")!=null) materialType	=map.get("order_s_material_type").toString().split(",");
		/*订单数*/
		String[] orderTotal	= null;
		if(map.get("order_s_order_total")!=null)orderTotal=map.get("order_s_order_total").toString().split(",");
		/*物料所属仓*/
		String[] materialFactory=null;
		if(map.get("order_s_material_house")!=null)materialFactory=map.get("order_s_material_house").toString().split(",");

		/*每列有几个子列*/
		String[] tableSourceArr	=null;
		if(map.get("order_s_table_source_arr")!=null)tableSourceArr=map.get("order_s_table_source_arr").toString().split(",");
		/*order_s_mtlkt_total	每条料可开数*/
		String[] mtlktTotal	=null;
		if(map.get("order_s_mtlkt_total")!=null)mtlktTotal=map.get("order_s_mtlkt_total").toString().split(",");
		/*order_s_klfs_way	开料方式*/
		String[] klfsWay	=null;
		if(map.get("order_s_klfs_way")!=null)klfsWay=map.get("order_s_klfs_way").toString().split(",");
		/*order_s_ylts_total	用料条数*/
		String[] yltsTotal	=null;
		if(map.get("order_s_ylts_total")!=null)yltsTotal=map.get("order_s_ylts_total").toString().split(",");
		/*order_s_klcc_size	开料尺寸*/
		String[] klccSize	=null;
		if(map.get("order_s_klcc_size")!=null)klccSize=map.get("order_s_klcc_size").toString().split(",");
		/*order_s_small_order_total	每列订单数*/
		String[] smallOrderTotal	=null;
		if(map.get("order_s_small_order_total")!=null)smallOrderTotal=map.get("order_s_small_order_total").toString().split(",");
		/*order_s_small_feed_total	每列投料数*/
		String[] smallFeedTotal	=null;
		if(map.get("order_s_small_feed_total")!=null)smallFeedTotal=map.get("order_s_small_feed_total").toString().split(",");
		//拼料颜色
		String[] plys=null;
		if(map.get("order_s_plys")!=null)plys=map.get("order_s_plys").toString().split(",");
		//SKU号
		String[] skuNo=null;
		if(map.get("order_s_sku_no")!=null)skuNo=map.get("order_s_sku_no").toString().split(",");
		//单只条码贴
		String[] singleCode=null;
		if(map.get("order_s_single_code")!=null)singleCode=map.get("order_s_single_code").toString().split(",");
		//内盒商品标识贴
		String[] nhspCode=null;
		if(map.get("order_s_nhsp_code")!=null)nhspCode=map.get("order_s_nhsp_code").toString().split(",");
		//外箱商品标识贴
		String[] wxspCode=null;
		if(map.get("order_s_wxsp_code")!=null)wxspCode=map.get("order_s_wxsp_code").toString().split(",");
		//客户PO号
		String[] custPoNo=null;
		if(map.get("order_s_cust_po_no")!=null)custPoNo=map.get("order_s_cust_po_no").toString().split(",");
		//说明
		String[] remark=null;
		if(map.get("order_s_remark")!=null)remark=map.get("order_s_remark").toString().split(",");
		//镜片材质
		String[] glassesType=null;
		if(map.get("order_s_glasses_type")!=null)glassesType=map.get("order_s_glasses_type").toString().split(",");
		//透光率
		String[] lightTrans=null;
		if(map.get("order_s_light_trans")!=null)lightTrans=map.get("order_s_light_trans").toString().split(",");
		//位置
		String[] location=null;
		if(map.get("order_s_location")!=null)location=map.get("order_s_location").toString().split(",");
		//颜色
		String[] otherColor=null;
		if(map.get("order_s_other_color")!=null)otherColor=map.get("order_s_other_color").toString().split(",");
		//内容
		String[] content=null;
		if(map.get("order_s_content")!=null)content=map.get("order_s_content").toString().split(",");
		//尺寸
		String[] size=null;
		if(map.get("order_s_size")!=null)size=map.get("order_s_size").toString().split(",");
		//材质
		String[] qualityType=null;
		if(map.get("order_s_quality_type")!=null)qualityType=map.get("order_s_quality_type").toString().split(",");
		//字体
		String[] font=null;
		if(map.get("order_s_font")!=null)font=map.get("order_s_font").toString().split(",");
		//工艺
		String[] arts=null;
		if(map.get("order_s_arts")!=null)arts=map.get("order_s_arts").toString().split(",");
		//点漆色
		String[] dqs=null;
		if(map.get("order_s_dqs")!=null)dqs=map.get("order_s_dqs").toString().split(",");
		//烫印色
		String[] tys=null;
		if(map.get("order_s_tys")!=null)tys=map.get("order_s_tys").toString().split(",");
		//印字色
		String[] yzs=null;
		if(map.get("order_s_yzs")!=null)yzs=map.get("order_s_yzs").toString().split(",");
		//读取颜色 //20190330 去除模板控制物料类型
		/*String[] isReadColor=null;
		if (map.get("order_s_isReadColor")!=null) {
			isReadColor=map.get("order_s_isReadColor").toString().split(",");
		}*/
		//大货日期  special_dh_date
		String[] special_dh_date=null;
		int specialDhDateLengthIndex=0;//当前大货下标 特殊处理
		if (map.get("special_dh_date")!=null) {
			special_dh_date=map.get("special_dh_date").toString().split("&");
		}
		//大货数量  order_s_dh_total
		String[] order_s_dh_total=null;
		if (map.get("order_s_dh_total")!=null) {
			order_s_dh_total=map.get("order_s_dh_total").toString().split(",");
		}
		String order_s_table_source_arr_str=map.get("order_s_table_source_arr_str").toString();
		List<List<String[]>> listArrStrList=ArrayUtils.arrayStrToArrayObj(order_s_table_source_arr_str);
		boolean addlist=true;
		for(int materialTypeIndex=0;materialTypeIndex<listArrStrList.size();materialTypeIndex++) {
			List<String[]> listArrStr = listArrStrList.get(materialTypeIndex);
			for (int i = 0; i < lineNo.length; i++) { //几行
				for (int j = 0; j < listArrStr.size(); j++) { //每列
					bean = new OrderInfoDetailBean();
					for (int j2 = 0; j2 < listArrStr.get(j).length; j2++) {//有几个小列
						switch (listArrStr.get(j)[j2].replaceAll("'", "")) {//每个小列是什么
							case "order_s_material_color"://有色号就有code
								bean.setColor(materialColor[0].trim());materialColor=ArrayUtils.delete(0, materialColor);
								bean.setMaterialCode(materialCode[0].trim());materialCode=ArrayUtils.delete(0, materialCode);	
								break;
							case "order_s_material_description":
								bean.setColorDescription(materialColorDescription[0].trim());materialColorDescription=ArrayUtils.delete(0, materialColorDescription);
								break;
							case "order_s_material_model":
								bean.setMaterialModel(materialModel[0].trim());materialModel=ArrayUtils.delete(0, materialModel);
								break;
							case "order_s_material_name":
								bean.setMaterialName(materialName[0].trim());materialName=ArrayUtils.delete(0, materialName);
								break;
							case "order_s_mtlkt_total":
								bean.setMtlktTotal(mtlktTotal[0]);mtlktTotal=ArrayUtils.delete(0, mtlktTotal);
								break;
							case "order_s_klfs_way":
								bean.setKlfsWay(klfsWay[0]);klfsWay=ArrayUtils.delete(0, klfsWay);
								break;
							case "order_s_small_order_total":
								if(smallOrderTotal[0].trim().length()==0) {
									bean.setSmallOrderTotal(null);smallOrderTotal=ArrayUtils.delete(0, smallOrderTotal);
								}else {
									bean.setSmallOrderTotal(smallOrderTotal[0]);smallOrderTotal=ArrayUtils.delete(0, smallOrderTotal);
								}
								break;
							case "order_s_small_feed_total":
								if(smallFeedTotal[0].trim().length()==0) {
									bean.setSmallFeedTotal(null);smallFeedTotal=ArrayUtils.delete(0, smallFeedTotal);
								}else {
									bean.setSmallFeedTotal(smallFeedTotal[0]);smallFeedTotal=ArrayUtils.delete(0, smallFeedTotal);
								}
								break;
							case "order_s_klcc_size":
								bean.setKlccSize(klccSize[0]);klccSize=ArrayUtils.delete(0, klccSize);
								break;
							case "order_s_ylts_total":
								bean.setYltsTotal(yltsTotal[0]);yltsTotal=ArrayUtils.delete(0, yltsTotal);
								break;
							case "order_s_plys":
								bean.setPlys(plys[0]);plys=ArrayUtils.delete(0, plys);
								break;
							case "order_s_sku_no":
								bean.setSkuNo(skuNo[0]);skuNo=ArrayUtils.delete(0, skuNo);
								break;
							case "order_s_single_code":
								bean.setSingleCode(singleCode[0]);singleCode=ArrayUtils.delete(0, singleCode);
								break;
							case "order_s_nhsp_code":
								bean.setNhspCode(nhspCode[0]);nhspCode=ArrayUtils.delete(0, nhspCode);
								break;
							case "order_s_wxsp_code":
								bean.setWxspCode(wxspCode[0]);wxspCode=ArrayUtils.delete(0, wxspCode);
								break;
							case "order_s_cust_po_no":
								bean.setCustPoNo(custPoNo[0]);custPoNo=ArrayUtils.delete(0, custPoNo);
								break;
							case "order_s_remark":
								bean.setRemark(remark[0]);remark=ArrayUtils.delete(0, remark);
								break;
							case "order_s_glasses_type":
								bean.setGlassesType(glassesType[0]);glassesType=ArrayUtils.delete(0, glassesType);
								break;
							case "order_s_light_trans":
								bean.setLightTrans(lightTrans[0]);lightTrans=ArrayUtils.delete(0, lightTrans);
								break;
							case "order_s_location":
								bean.setLocation(location[0]);location=ArrayUtils.delete(0, location);
								break;
							case "order_s_other_color":
								bean.setOtherColor(otherColor[0]);otherColor=ArrayUtils.delete(0, otherColor);
								break;
							case "order_s_content":
								bean.setContent(content[0]);content=ArrayUtils.delete(0, content);
								break;
							case "order_s_size":
								bean.setSize(size[0]);size=ArrayUtils.delete(0, size);
								break;
							case "order_s_quality_type":
								bean.setQualityType(qualityType[0]);qualityType=ArrayUtils.delete(0, qualityType);
								break;
							case "order_s_font":
								bean.setFont(font[0]);font=ArrayUtils.delete(0, font);
								break;
							case "order_s_arts":
								bean.setArts(arts[0]);arts=ArrayUtils.delete(0, arts);
								break;
							case "order_s_dqs":
								bean.setDqs(dqs[0]);dqs=ArrayUtils.delete(0, dqs);
								break;
							case "order_s_tys":
								bean.setTys(tys[0]);tys=ArrayUtils.delete(0, tys);
								break;
							case "order_s_yzs":
								bean.setYzs(yzs[0]);yzs=ArrayUtils.delete(0, yzs);
								break;
							case "order_s_dh_total"://大货数量
								bean.setDhTotal((order_s_dh_total[0].trim().length()>0)?order_s_dh_total[0]:"0");order_s_dh_total=ArrayUtils.delete(0, order_s_dh_total);
								bean.setDhDate(special_dh_date[materialTypeIndex].split(",")[specialDhDateLengthIndex]);
								specialDhDateLengthIndex++;
								if(specialDhDateLengthIndex>special_dh_date[materialTypeIndex].split(",").length-1) {
									specialDhDateLengthIndex=0;
								}
								break;
							default:
								break;
							}
					}
					bean.setLineNo(lineNo[i]);
					//投料数外部有 取外部 外部没有取内部
					if(bean.getSmallFeedTotal()!=null) {//内部有投料数
						bean.setFeedTotal(bean.getSmallFeedTotal());
					}else if(feedTotal!=null&&feedTotal.length>((materialTypeIndex>0)?lineNo.length+i:i)) {//内部没有 外部有
						/*此处为物料大组件一二都有外部投料数情况*/
						bean.setFeedTotal(feedTotal[(materialTypeIndex>0)?lineNo.length+i:i].trim().length()>0?feedTotal[(materialTypeIndex>0)?lineNo.length+i:i]:null);
					}else if(feedTotal!=null&&"投料数".equals(materialType[(materialTypeIndex>0)?listArrStrList.get(0).size()+j:j])){
						/*此处为物料大组件一或则二有外部投料数情况*/
						bean.setFeedTotal(feedTotal[i].trim().length()>0?feedTotal[i]:null);
					}else {//内部外部都没有
						bean.setFeedTotal(bean.getSmallFeedTotal());
					}
					//订单数外部有 取外部 外部没有取内部
					if(bean.getSmallOrderTotal()!=null) {//内部有订单数
						bean.setOrderTotal(bean.getSmallOrderTotal());
					}else if(orderTotal!=null&&orderTotal.length>((materialTypeIndex>0)?lineNo.length+i:i)) {//内部没有订单数  外部有 
						/*此处为物料大组件一二都有外部订单数情况*/
						bean.setOrderTotal(orderTotal[(materialTypeIndex>0)?lineNo.length+i:i].trim().length()>0?orderTotal[(materialTypeIndex>0)?lineNo.length+i:i]:null);
					}else if(orderTotal!=null&&"订单数".equals(materialType[(materialTypeIndex>0)?listArrStrList.get(0).size()+j:j])){
						/*此处为物料大组件一或则二有外部订单数情况*/
						bean.setOrderTotal(orderTotal[i].trim().length()>0?orderTotal[i]:null);
					}else {//内外都没有
						bean.setOrderTotal(bean.getSmallOrderTotal());
					}
					
					bean.setOrderNo(orderNo);
					if(materialFactory==null) {//旧模版默认为原材料仓
						bean.setMaterialFactory("1");
					}else {
						bean.setMaterialFactory(materialFactory[(materialTypeIndex>0)?listArrStrList.get(0).size()+j:j]);
					}
					bean.setMaterialType(materialType[(materialTypeIndex>0)?listArrStrList.get(0).size()+j:j]);
					//20190330 去除模板控制物料类型
					/*if (isReadColor!=null&&isReadColor[(materialTypeIndex>0)?listArrStrList.get(0).size()+j:j].trim().length()>0) {
						bean.setIsReadColor(Integer.valueOf(isReadColor[(materialTypeIndex>0)?listArrStrList.get(0).size()+j:j]));
					}*/
					//校验是否为0 避免新模板保存的时候出现的问题
					if(bean.getIsReadColor()==0) {
						map.put("material_type", bean.getMaterialType());
						map.put("material_factory", bean.getMaterialFactory());
						Map<String, String> materialTypeInfo = orderCreateMapper.getMaterialTypeInfo(map);
						bean.setIsReadColor(Integer.valueOf(materialTypeInfo.get("isReadColor")));
					}
					if(lineNoCodeMap!=null) {
						//通过物料代码补全物料信息
						fixMaterialInfo(bean,lineNoCodeMap,map);
					}
					list.add(bean);
				}
			}
		}
		
		
		/*int index = 0;
		for(int i=0;i<lineNo.length;i++) {
			for(int j=0;j<tableSourceArr.length;j++) {
				int index_data=i*tableSourceArr.length+j;
				bean = new OrderInfoDetailBean();
				bean.setColor(materialColor[index_data]);
				bean.setColorDescription(materialColorDescription[index_data]);
				bean.setFeedTotal(feedTotal[i]);
				bean.setLineNo(lineNo[i]);
				bean.setMaterialCode(materialCode[index_data]);
				bean.setMaterialFactory("1");
				if("2".equals(tableSourceArr[j])){//无规格
					bean.setMaterialModel("");
				}else{
					bean.setMaterialModel(materialModel[index]);
					index++;
				}
				bean.setMaterialType(materialType[j]);
				bean.setOrderNo(orderNo);
				bean.setOrderTotal(orderTotal[i]);
				System.out.println(bean);
				list.add(bean);
			}
		}*/
		return list;
	}

	//获取固定值
	public Map<String, String> getOrderInfoFixed(Map map) {
		// TODO Auto-generated method stub
		map = orderCreateMapper.getOrderInfoFixed(map);
		return map;
	}

	public JSONObject getOrderInfoForTemp(Map<String, String> map) {
		Map<String,String> map_orderInfo = orderCreateMapper.getOrderInfoByOrderNo(map.get("order_no").toString());
		List<Map<String, Object>> map_orderInfoDetail =orderCreateMapper.getOrderInfoDetailByOrderNo(map.get("order_no").toString());
		List<Map<String, Object>> map_orderInfoTail =orderCreateMapper.getOrderInfoTailByOrderNo(map.get("order_no").toString());
		Map<String,String> map_orderCustomTag = orderCreateMapper.getOrderCustomTagByOrderNo(map.get("order_no").toString());

		JSONObject object = new JSONObject();
		object.put("orderInfo", map_orderInfo);//订单信息主表
		//增加别处写入order_info_detail 避免出现物料序号混乱修正
		List<Map<String, Object>> map_orderInfoDetailByFixMaterialAndLineNo = getOrderInfoDetailByFixMaterialAndLineNo(map_orderInfoDetail,object,map.get("order_s_table_source_arr_str"));
		object.put("orderInfoDetail", map_orderInfoDetailByFixMaterialAndLineNo);//订单信息明细表
		if (map.get("sort_order_s_input_material_type")!=null) {
			List<Map<String, Object>> sort_map_orderInfoTail=getSortMapOrderInfoTail(map,map_orderInfoTail);
			object.put("orderInfoTail", sort_map_orderInfoTail);
		}else {
			object.put("orderInfoTail", map_orderInfoTail);
		}
		object.put("orderCustomTag", map_orderCustomTag);
		return object;
	}
	/**
	 * 订单明细其他地方补充重新调整顺序避免在模板上出现数据错乱
	 * 通过物料 仓位 序号
	 * @param map_orderInfoDetail
	 * @return
	 */
	private List<Map<String, Object>> getOrderInfoDetailByFixMaterialAndLineNo(List<Map<String, Object>> map_orderInfoDetail,JSONObject object,String order_s_table_source_arr_str){
		if(order_s_table_source_arr_str.length()<1) {//如果页面没有物料大组件
			return null;
		}
		List<List<String[]>> listArrStrList=ArrayUtils.arrayStrToArrayObj(order_s_table_source_arr_str);//获取每份几个物料类型
		int splitMaterialIndex = listArrStrList.get(0).size();
		
		List<Map<String, Object>> returnMap = new ArrayList<>();
		if(map_orderInfoDetail.size()<1) {
			return returnMap;
		}
		
		List<String> recordMaterialType = new ArrayList<>();//记录物料类型
		List<String> recordMaterialFactory = new ArrayList<>();//记录物料仓位
		List<String> recordIsReadlor = new ArrayList<>();//记录物料仓位
		String outSideFeedTotal="0";
		//int materialTypeIndex=0;//记录当前recordMaterialType的下标 从第二行开始
		//初始化物料类型 仓位等数据 并将第一行数据放入返回map中
		int indexMaterialType=0;
		for (int i = 0; i < map_orderInfoDetail.size(); i++) {
			Map<String,Object> everyMap=null;//当前
			//System.out.println(1 == Integer.valueOf(map_orderInfoDetail.get(i).get("order_s_line_no").toString()));
			if(1 == Integer.valueOf(map_orderInfoDetail.get(i).get("order_s_line_no").toString())) {//第一行 记录所有物料类型
				recordMaterialType.add(map_orderInfoDetail.get(i).get("order_s_material_type").toString());
				recordMaterialFactory.add(map_orderInfoDetail.get(i).get("order_s_material_house").toString());
				recordIsReadlor.add(map_orderInfoDetail.get(i).get("order_s_isReadColor").toString());
				everyMap=map_orderInfoDetail.get(i);
				//everyMap.put("sortMaterialType", indexMaterialType);//排序使用
				String lineNo = String.format("%02d",Integer.valueOf(map_orderInfoDetail.get(i).get("order_s_line_no").toString()));
				String indexMaterialTypeStr=String.format("%02d",indexMaterialType);
				if(indexMaterialType<=splitMaterialIndex-1) {
					everyMap.put("sortMaterialTypeTable", 1+lineNo+indexMaterialTypeStr);//排序使用
				}else {
					everyMap.put("sortMaterialTypeTable", 2+lineNo+indexMaterialTypeStr);//排序使用
				}
				indexMaterialType++;
				if(everyMap.get("order_s_feed_total")!=null) {
					outSideFeedTotal=everyMap.get("order_s_feed_total").toString();
				}
				returnMap.add(everyMap);//加入到返回值中
			}
		}
		//当前执行行号
		//int currentLineNo=2;
		int order_s_line_no_total= Integer.valueOf(map_orderInfoDetail.get(map_orderInfoDetail.size()-1).get("order_s_line_no").toString());
		object.put("order_s_line_no_total", order_s_line_no_total);//总行号
		String[] map_orderInfoDetailArr = new String[map_orderInfoDetail.size()];
		for (int currentLineNo = 2; currentLineNo < order_s_line_no_total+1; currentLineNo++) {//有几行
			for(int j=0;j<recordMaterialType.size();j++) {//物料类型/每行循环
				Map<String,Object> everyMap=null;//当前
				for (int i = 0; i < map_orderInfoDetail.size(); i++) {
					if(Integer.valueOf(currentLineNo).equals(Integer.valueOf(map_orderInfoDetail.get(i).get("order_s_line_no").toString()))) {//从第二行开始 
						if(recordMaterialType.get(j).equals(map_orderInfoDetail.get(i).get("order_s_material_type"))
								&&Integer.valueOf(recordMaterialFactory.get(j)) == Integer.valueOf(map_orderInfoDetail.get(i).get("order_s_material_house").toString())) {//如果物料信息和订单明细数据相同 放入 break跳出循环
							if(map_orderInfoDetailArr[i]!=null) {
								continue;
							}
							everyMap=map_orderInfoDetail.get(i);
							map_orderInfoDetailArr[i]="";
							break;
						}
					}
				}
				//每一次循环为了找到这个物料类型在当前行号中是否有
				if(everyMap!=null) {
					//everyMap.put("sortMaterialType", j);
					String lineNo = String.format("%02d",Integer.valueOf(everyMap.get("order_s_line_no").toString()));
					String indexMaterialTypeStr=String.format("%02d",j);
					if(j<=splitMaterialIndex-1) {
						everyMap.put("sortMaterialTypeTable", 1+lineNo+indexMaterialTypeStr);//排序使用
					}else {
						everyMap.put("sortMaterialTypeTable", 2+lineNo+indexMaterialTypeStr);//排序使用
					}
					if(everyMap.get("order_s_feed_total")!=null) {
						outSideFeedTotal=everyMap.get("order_s_feed_total").toString();
					}
					returnMap.add(everyMap);//加入到返回值中
				}else {//如果物料信息和订单明细数据不相同 放入空map并赋值 直到物料信息匹配跳出
					Map<String,Object> orderInfoDetaiMapNull = getOrderInfoDetaiMapNull();//获取一个空的订单明细map
					//orderInfoDetaiMapNull.put("sortMaterialType", j);
					orderInfoDetaiMapNull.put("order_s_line_no", currentLineNo);//行号
					orderInfoDetaiMapNull.put("order_s_material_type", recordMaterialType.get(j));//物料类型
					orderInfoDetaiMapNull.put("order_s_material_house", recordMaterialFactory.get(j));//仓位
					orderInfoDetaiMapNull.put("order_s_isReadColor", recordIsReadlor.get(j));//是否读取颜色
					String lineNo = String.format("%02d",currentLineNo);
					String indexMaterialTypeStr=String.format("%02d",j);
					if(j<=splitMaterialIndex-1) {
						orderInfoDetaiMapNull.put("sortMaterialTypeTable", 1+""+lineNo+""+indexMaterialTypeStr);//排序使用
					}else {
						orderInfoDetaiMapNull.put("sortMaterialTypeTable", 2+""+lineNo+""+indexMaterialTypeStr);//排序使用
					}
					if(recordMaterialType.get(j).equals("投料数")) {//如果有外部投料数需要增加
						orderInfoDetaiMapNull.put("order_s_feed_total", outSideFeedTotal);
						outSideFeedTotal="";
					}
					returnMap.add(orderInfoDetaiMapNull);//加入到返回值中
				}
			}
		}
		
		//System.out.println(returnMap.size());
		Collections.sort(returnMap, new Comparator<Map<String,Object>>(){

			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				// TODO Auto-generated method stub
				Integer o1Int = Integer.valueOf(o1.get("sortMaterialTypeTable").toString());
				Integer o2Int = Integer.valueOf(o2.get("sortMaterialTypeTable").toString());
				return o1Int.compareTo(o2Int);
			}
			
		});
		//获取特殊的数据
		//大货时间
		String specialDhDate="";
		for(int i =0;i<returnMap.size();i++) {
			//第一行并且有大货时间
			if(returnMap.get(i).get("special_dh_date")!=null&&returnMap.get(i).get("special_dh_date").toString().trim().length()>0&&"1".equals(returnMap.get(i).get("order_s_line_no").toString())) {
				specialDhDate+=returnMap.get(i).get("special_dh_date")+",";
			}
			System.out.println(returnMap.get(i).get("order_s_material_type")+""+returnMap.get(i).get("sortMaterialTypeTable"));
		}
		object.put("special_dh_date", (specialDhDate.length()>0)?specialDhDate.substring(0,specialDhDate.length()-1):specialDhDate);
		return returnMap;//map_orderInfoDetail
	}
	/**
	 * 得到一个空的订单明细map
	 * @param map
	 * @param source_map_orderInfoTail
	 * @return
	 */
	private Map<String,Object> getOrderInfoDetaiMapNull(){
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("order_s_arts", "");		returnMap.put("order_s_content", "");		returnMap.put("order_s_cust_po_no", "");
		returnMap.put("order_s_dqs", "");		returnMap.put("order_s_feed_total", "");		returnMap.put("order_s_font","");
		returnMap.put("order_s_isReadColor", "");		returnMap.put("order_s_klcc_size", "");		returnMap.put("order_s_klfs_way", "");
		returnMap.put("order_s_light_trans","");		returnMap.put("order_s_line_no", "");		returnMap.put("order_s_location", "");
		returnMap.put("order_s_material_code", "");		returnMap.put("order_s_material_color", "");		returnMap.put("order_s_material_description", "");
		returnMap.put("order_s_material_factory", "");		returnMap.put("order_s_material_house", "");		returnMap.put("order_s_material_model", "");
		returnMap.put("order_s_material_name", "");		returnMap.put("order_s_material_type", "");		returnMap.put("order_s_mtlkt_total", "");
		returnMap.put("order_s_nhsp_code", "");		returnMap.put("order_s_order_total", "");		returnMap.put("order_s_other_color", "");
		returnMap.put("order_s_plys", "");		returnMap.put("order_s_quality_type", "");		returnMap.put("order_s_remark", "");
		returnMap.put("order_s_single_code", "");		returnMap.put("order_s_size", "");		returnMap.put("order_s_sku_no", "");
		returnMap.put("order_s_small_feed_total", "");		returnMap.put("order_s_small_order_total", "");		returnMap.put("order_s_tys", "");returnMap.put("order_s_yzs", ""); 
		returnMap.put("order_s_wxsp_code", "");		returnMap.put("order_s_ylts_total", "");
		return returnMap;
	}
	/*
	 * 流程卡特有 sort_order_s_input_material_type
	 * 通过排序避免数据顺序问题导致流程卡数据赋值出现顺序问题
	 * */
	private List<Map<String, Object>> getSortMapOrderInfoTail(Map<String, String> map,List<Map<String, Object>> source_map_orderInfoTail) {
		List<Map<String, Object>> sort_map_orderInfoTail = new ArrayList<>();
		String[] order_s_input_material_house_arr=map.get("order_s_input_material_house").toString().split(",");
		String[] order_s_input_material_type_arr =map.get("order_s_input_material_type").toString().split(",");
		Map<String, Object> temp_map=null;
		for(int i=0;i<order_s_input_material_house_arr.length;i++) {
			temp_map=new HashMap<>();
			temp_map.put("order_s_input_material_code", " ");
			temp_map.put("order_s_input_material_house", order_s_input_material_house_arr[i]);
			temp_map.put("order_s_input_material_type", order_s_input_material_type_arr[i]);
			temp_map.put("order_s_input_val_material_type", "");
			sort_map_orderInfoTail.add(i,temp_map);
			for(Map<String, Object> source_map:source_map_orderInfoTail) {
				if (source_map.get("order_s_input_material_house").toString().equals(order_s_input_material_house_arr[i].toString())
						&&
						source_map.get("order_s_input_material_type").toString().equals(order_s_input_material_type_arr[i].toString())	) {
					sort_map_orderInfoTail.remove(i);
					sort_map_orderInfoTail.add(source_map);
					break;
				}
			}
		}
		
		return sort_map_orderInfoTail;
	}

	public int checkTempAndOrderNo(Map<String, Object> map) {
		return orderCreateMapper.checkTempAndOrderNo(map);
	}
	//查询订单是否已审核
	public int getAuditTotalByOrderNo(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return orderCreateMapper.getAuditTotalByOrderNo(map);
	}
	//更新订单 --审核订单使用
	public int updateAudit(Map map) {
		// TODO Auto-generated method stub
		if("1".equals(map.get("order_p_audit").toString())) {
			map.put("order_p_audit", "1");
			map.put("order_p_order_status", "审核");
			map.put("status", "1");
			
			final Object context = "上下文信息";
	        new CallBackTask(new CallBackBody() {
	            @Override
				public
	            void execute(Object context) throws Exception {
	    			orderProcedureTemplateService.addOrderProcedureBySameOrder(map.get("order_p_order_no").toString(), map.get("loginCompany").toString(), map.get("order_p_product_no").toString());
	            }
	        }).start(context);
	        
		}else if("0".equals(map.get("order_p_audit").toString())){
			map.put("order_p_audit", "0");
			map.put("order_p_order_status", "计划");
			map.put("status", "0");
		}else{
			map.put("order_p_audit", "-1");
			map.put("order_p_order_status", "退回");
			map.put("status", "0");
		}
		map.put("order_no", map.get("order_p_order_no").toString());
		orderCreateApplyService.sendTemplateMsg("audit",map);
		return orderCreateMapper.updateAudit(map);
	}
	
	public int deleteTemplate(Map<String, Object> map) {
		return orderCreateMapper.deleteTemplate(map);
	}
	//获取部门信息表
	public List<Map> selectOnlineByType(Map map) {
		// TODO Auto-generated method stub
		if("order_p_deptid".equals(map.get("type"))) {
			return orderCreateMapper.selectOnlineByDeptId(map);
		}
		if("order_p_product_work_no".equals(map.get("type"))) {
			return orderCreateMapper.selectOnlineForproductWorkNo(map);
		}
		return null;
	}
	//通过参数获取生产订单主表数据
	public List<Map> getOrderInfoByParam(Map map) {
		// TODO Auto-generated method stub
		return orderCreateMapper.getOrderInfoByParam(map);
	}

	public int deleteOrderInfoPrimary(Map map) {
		// TODO Auto-generated method stubupdateAudit
		return orderCreateMapper.updateOrderInfoPrimary(map);
	}

	public Map<String, String> getOrderInfoFixedPermission(Map map) {
		// TODO Auto-generated method stub
		return orderCreateMapper.getOrderInfoFixedPermission(map);
	}
	//订单添加操作日志
	public void operateLog(Map map) {
		orderCreateMapper.operateLog(map);
	}
	
	public List<Map> getOrderNoByBussNo(Map map) {
		// TODO Auto-generated method stub
		return orderCreateMapper.getOrderNoByBussNo(map);
	}
	
	/**
	 * 将序号物料代码转换成code
	 * @param map
	 * @return
	 */
	public Map<String,String> getLineNoCode(Map map){
		Map<String,String> lineNoCode = null;
		if(map.get("line_no_material_code")!=null&&map.get("line_no_material_code").toString().trim().length()>0) {
			lineNoCode = new HashMap<>();
			String[] lineNoCodeArr = map.get("line_no_material_code").toString().split(",");
			for(int i=0 ;i<lineNoCodeArr.length;i++) {
				String[] lineNoCodeStr=lineNoCodeArr[i].split("@");
				if(lineNoCodeStr.length==2) {
					lineNoCode.put(lineNoCodeStr[0], lineNoCodeStr[1]);
				}else {
					lineNoCode.put(lineNoCodeStr[0], "");
				}
			}
		}
		return lineNoCode;
	}
	/**
	 * 补全物料信息 通过物料表
	 */
	public  void fixMaterialInfo(OrderInfoDetailBean bean,Map lineNoCode, Map requestMap) {
		//拼接key 序号|物料类型|仓位
		String  key = bean.getLineNo()+"|"+bean.getMaterialType()+"|"+bean.getMaterialFactory();
		//通过lineNoCode获取物料代码
		if(lineNoCode.get(key)!=null&&lineNoCode.get(key).toString().trim().length()>0) {
			String materialCode = lineNoCode.get(key).toString().trim();
			//获取物料信息
			OrderInfoDetailBean beanByCode = orderCreateMapper.getMaterialInfoByCode(materialCode,requestMap);
			if(beanByCode!=null) {
				bean.setMaterialCode(beanByCode.getMaterialCode());//物料代码
				if (1==beanByCode.getIsReadColor()) {
					if (bean.getIsReadColor()==0) {
						bean.setIsReadColor(1);
					}
					bean.setColor(beanByCode.getColor());//色号
					//bean.setColorDescription(beanByCode.getColorDescription());//描述
				}else {
					if (bean.getIsReadColor()==0) {
						bean.setIsReadColor(2);
					}
				}
				bean.setMaterialModel(beanByCode.getMaterialModel());//规格
				bean.setMaterialName(beanByCode.getMaterialName());//物料名称
			}
		}
		
	}
	/**
	 * 获取dialog选择物料total
	 * @param map
	 * @return
	 */
	public int getFactoryTabChildListTotal(Map<String, String> map) {
		// TODO Auto-generated method stub
		
		return orderCreateMapper.getFactoryTabChildListTotal(map);
	}
	/**
	 * 获取物料工艺列表
	 * @param map
	 * @return
	 */
	public List<Map> getArtsList(Map map) {
		return orderCreateMapper.getArtsList(map);
	}
	/**
	 * 校验订单是否申请中 并返回申请信息
	 */
	public List<Map<String,String>> getMyApplyInfo(String orderNo) {
		return orderCreateMapper.getMyApplyInfo(orderNo);
	}

	public List<Map> getCompanyMaterialDeptInfo(Map map) {
		// TODO Auto-generated method stub
		return basicDataMapper.getCompanyMaterialDeptInfoList(map);
	}
	/**
	 * 批量保存设备订单
	 * @param orderNo
	 * @return
	 */
	public int insertOrderBatchForDevice(Map map) {
		// TODO Auto-generated method stub
		List<OrderDeviceBean> orderDeviceBeans = com.alibaba.fastjson.JSONArray.parseArray(map.get("orderdevicelist").toString()).toJavaList(OrderDeviceBean.class);
		return orderCreateMapper.insertOrderBatchForDevice(map,orderDeviceBeans);
	}

	public int updateOrderBatchForDevice(Map map) {
		List<OrderDeviceBean> orderDeviceBeans = com.alibaba.fastjson.JSONArray.parseArray(map.get("orderdevicelist").toString()).toJavaList(OrderDeviceBean.class);
		return orderCreateMapper.updateOrderBatchForDevice(map,orderDeviceBeans);
	}
	/**
	 * 查询设备订单
	 * @param map
	 * @return
	 */
	public DatatablesViewPageBean selectOrderBatchForDevice(Map map) {
		// TODO Auto-generated method stub
		DatatablesViewPageBean bean = new DatatablesViewPageBean();
		bean.setReturnData(orderCreateMapper.getOrderBatchForDeviceList(map));
		int total = orderCreateMapper.getOrderBatchForDeviceTotal(map);
		bean.setiTotalDisplayRecords(total);
		bean.setiTotalRecords(total);
		return bean;
	}
	
}
